﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Kugar.Core.ExtMethod;
using Kugar.Core.WinForm;

namespace Kugar.ERPFramework.View.WinForm
{
    public class FilterConditionCollection:IEnumerable<KeyValuePair<string, ConditionItem>>
    {
        private Dictionary<string,ConditionItem> _cacheCondition=new Dictionary<string, ConditionItem>();

        public bool Add(string name,string text, Type dataType)
        {
            if (_cacheCondition.ContainsKey(name))
            {
                return false;
            }

            var item = new ConditionItem();

            item.DataType = dataType;
            item.Name = name;
            item.Text = text;

            _cacheCondition.Add(name,item);

            return true;
        }

        public bool AddDateTime(string name, string text, bool containsTime)
        {
            if (_cacheCondition.ContainsKey(name))
            {
                return false;
            }

            var item = new  ConditionItem_DateTime();
            item.Name = name;
            item.Text = text;
            item.ContainsTime = containsTime;

            _cacheCondition.Add(name,item);

            return true;
        }

        public bool Add(ConditionItem item)
        {
            if (_cacheCondition.ContainsKey(item.Name))
            {
                return false;
            }

            _cacheCondition.Add(item.Name,item);

            return true;
        }

        public int Count { get { return _cacheCondition.Count; } }

        public ConditionItem this[string name]
        {
            get { return _cacheCondition.TryGetValue(name, null); }
        }

        public bool Contains(string name)
        {
            return _cacheCondition.ContainsKey(name);
        }
            

        public IEnumerator<KeyValuePair<string, ConditionItem>> GetEnumerator()
        {
            return _cacheCondition.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }


    public class ConditionItem
    {
        public string Name { set; get; }
        public string Text { get; set; }
        public Type DataType { set; get; }

        public Control Label { set; get; }
        public Control Editor { set; get; }
    }

    public class ConditionItem_Numeric : ConditionItem
    {
        public ConditionItem_Numeric()
        {
            Digit = 0;
        }

        public int Digit { set; get; }
    }

    public class ConditionItem_DateTime : ConditionItem
    {
        public ConditionItem_DateTime()
        {
            DataType = typeof(DateTime);
        }

        public bool ContainsTime { set; get; }
    }

    public class ConditionItem_Enumable : ConditionItem
    {
        public ConditionItem_Enumable(IEnumerable<ComboItemEx> items)
            : base()
        {
            if (items == null || !items.Any())
            {
                throw new ArgumentOutOfRangeException("items");
            }

            Items = new List<ComboItemEx>(items);
        }

        public List<ComboItemEx> Items { get; private set; }
    }
}